﻿using MediatR;
using Warehouse.API.Applcation.Command.InventoryCommand;
using Warehouse.Infrstructre.Interface;

namespace Warehouse.API.Applcation.CommandHandlers.InventoryCommandHandler
{
    /// <summary>
    /// 删除仓库盘点中介者
    /// </summary>
    public class DelInventoryCommandHandler:IRequestHandler<DelInventoryCommand,int>
    {
        private readonly IInventoryRepository _inventRep;

        public DelInventoryCommandHandler(IInventoryRepository inventRep)
        {
            _inventRep = inventRep;
        }

        public async Task<int> Handle(DelInventoryCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var res = (await _inventRep.GetAllAsyns()).Where(x => request.ids.Contains(x.InventoryId)).ToList();
            if (res != null)
            {
                foreach (var item in res)
                {
                    item.IsDel = true;
                    item.DelName = request.DelName;
                    item.DelTime = DateTime.Now;
                    await _inventRep.UpdateAsync(item);
                    count = 1;
                }
            }
            else
            {
                count = -1;
            }
            return count;

            
        }
    }
}
